
var globleId, globlePlanId, globleFeatureId, globleProjectId;

unQueryFlag = true;
function memberPageselectCallback(page_index, jq) {
	if(unQueryFlag)
	{
		unQueryFlag = false;
		return false;
	}
	var fromValues = jQuery('#addMemberForm').formSerialize();
	$('#queryMemberDiv').load('../manager/user/projectMember.htm?' + fromValues, {
		pageNum : page_index,
		pageSize : 10
	});
}

function memberInitPagination(current_page) {
	var num_entries = $("#memberPageCount").val();
	$("#memberPagination").pagination(num_entries, {
		num_edge_entries : 2,
		current_page: current_page,
		num_display_entries : 4,
		callback : memberPageselectCallback,
		items_per_page : 10,
		prev_text : '上一页',
		next_text : '下一页'
	});
}

$(document).ready(
		function() {
			var departName = $("#hiddenDepartName").val();
			var departId = $("#hiddenDepartId").val();
			var defaultValue = "请选择部门..";
			var qmpDepartName = $("#hiddenQmpDepartName").val();
			var qmpDepartId = $("#hiddenQmpDepartId").val();
			var qmpProjectName = $("#hiddenQmpProjectName").val();
			var qmpProjectId = $("#hiddenQmpProjectId").val();
			var defaultQMPDeaprtValue = "请选择日报部门..";
			var defaultQMPProjectValue = "请选择日报项目..";
			var projectId = $("#projectId").val();
			
			if(projectId != ""){
				
				$("#projectMemberDiv").html("<div class='loadIcon'>页面加载中...</div>");
				
			}
			
			if(qmpDepartName !=""){
				defaultQMPDeaprtValue = qmpDepartName;
			}
		
			if(qmpProjectName !=""){
				defaultQMPProjectValue = qmpProjectName;
			}
			
			if (departName != "") {
				defaultValue = departName;
			}

			
			
		
			$("#addMemberDiv").dialog({
				resizable: false,
				autoOpen: false,
				height:420,
				width:740,
				modal: true,
				buttons: {
					'取消':function(){
					 $(this).dialog('close');
					},	
					'确定': function() {
						$(this).dialog('close');				
					}
								
				}
			});	

			$("#userRealName").autocomplete('/aonewebx/search.htm', {
				minChars : 1,
				autoFill : true,
				mustMatch : true,
				cachelenght:10,
				matchContains : true,
				scrollHeight : 220,
				dataType : 'json',
				parse : function(data) {
					var rows = [];
					for ( var i = 0; i < data.length; i++) {
						rows[rows.length] = {
							data : data[i],
							value : data[i],
							result : data[i]
						};
					}
					return rows;
				},
				formatItem : function(row, i, n) {
					return row;
				}
			});
			
		var queryMemberOptions = {
				target : '#queryMemberDiv',
				clearForm : false,
				resetForm : false,
				timeout : 3000,
				success : function() {
					$("#projectMemberTable tr").mouseover( function() {
						$(this).addClass("over");
					});
					$("#projectMemberTable tr").mouseout( function() {
						$(this).removeClass("over");
					});
					memberInitPagination(0);
				}
			};


		$("#queryUserBtn").click(function(){
			unQueryFlag = true;
			$("#addMemberForm").ajaxSubmit(queryMemberOptions);
			return false;
		})

		/* 点击增加测试人员 */
		$("#addProjectUser").click(function(){
			alert("good");
			$("#addMemberDepart").html("");
			$("#addMemberDepart").treeSelect( {
				jstree : {
					data : {
						opts : {
							url : '../depart/qualities.htm',
							method : 'get'
						}
					}
				},
				name : 'memberDepartId',
				defaultValue : defaultValue,
				width : 280
			});
			
			$("#memberDepartId").val(departId);
			
			var fromValues = $('#addMemberForm').formSerialize();
			$('#queryMemberDiv').load('../manager/user/projectMember.htm?' + fromValues, {
				pageNum : 0,
				pageSize : 10
				}, function(){
					$("#projectMemberTable tr").mouseover( function() {
						$(this).addClass("over");
					});
					$("#projectMemberTable tr").mouseout( function() {
						$(this).removeClass("over");
					});
					unQueryFlag = true;
					memberInitPagination(0);
			});
			
			$("#addMemberDiv").dialog("open");
		});
		
		//新增邮件接收人列表
		$('#addEmailRecipient').click(function() {
			
			$("#addMemberDepart").html("");
			$("#addMemberDepart").treeSelect( {
				jstree : {
					data : {
						opts : {
							url : '../depart/b2bTech.htm',
							method : 'get'
						}
					}
				},
				name : 'memberDepartId',
				defaultValue : defaultValue,
				width : 280
			});
			
			$("#memberDepartId").val(departId);
			
			var fromValues = $('#addMemberForm').formSerialize();

		});
	}
)

/**
 * 新增项目成员/新增邮件接收人
 * @return
 */
function addUser(){
	var members = "";
	var emailList = "";
	
	 $("[name='check_member']").each(function(){
		 if($(this).attr("checked")){
			 
			 //项目成员
			 if(members == "") {
				 members = $(this).attr("id");
			 }else{
				 members = members + "," + $(this).attr("id");
			 }
			 
			 //邮件列表
			 if(emailList == "") {
				 emailList = $(this).attr("realName") + ":" + $(this).attr("email");
			 } else {
				 emailList += ( ";" + $(this).attr("realName") + ":" +  $(this).attr("email") );
			 }
		 }		
	 })
	var projectId = $("#projectId").val();
	 
	if($('#memberOperType').val() == ADD_EMAIL_RECIPIENT) {
		
		$.post("addProjectEmailReceiver.htm",{emailList:emailList,projectId:projectId},function(){
			$('#emailSendersListShowDiv').load("emailSenderList.htm?projectId=" +  projectId);
			$("#message").html("新增邮件接收人成功!");
			$("#message").dialog("open");
		});
	} else {
		
		$.post("addProjectMember.htm",{members:members,projectId:projectId},function(){
			
			$("#projectMemberDiv").load("projectMember.htm?projectId=" +  projectId,function(){							
			});
			$("#message").html("成员增加成功!");
			$("#message").dialog("open");
			
		});
	}
}



function checkForm() {
	var result = {
		result : true,
		msg : ""
	};
	if($("#projectName").val()==""){
		result["result"] = false;
		result["msg"]= result["msg"] + "项目信息不能为空!<br>";
	}
	
	if($("#departId").val()==""){
		result["result"] = false;
		result["msg"]=result["msg"] + "部门信息不能为空!<br>";
	}
	
	if($("#qcRealProjectId").val()==""){
		result["result"] = false;
		result["msg"]=result["msg"] + "QC实体项目信息不能为空!<br>";
	}
	return result;
}


